package com.example.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;

@Configuration
public class CrosConfig implements WebMvcConfigurer {
    @Value("${fileUpload.rootSavePath}")
    private String rootSavePath;

    /**
     * 重写这个方法即可配置虚拟路径
     * registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/images/**").addResourceLocations("file:F:\\java代码\\mall_image\\");

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                //支持跨域的URL
                .addMapping("/**")
                //是否发送 Cookie
                .allowCredentials(true)
                //放行的源
                .allowedOrigins("http://127.0.0.1:8848")
                //容许的方法
                .allowedMethods("GET","POST","PUT","DELETE")
                //容许的请求头
                .allowedHeaders("*");
    }

}
